//
//  JPComposeEmotionKeyBoard.swift
//  JPWeibo
//
//  Created by 哇咔咔 on 2016/10/6.
//  Copyright © 2016年 jp. All rights reserved.
//

import UIKit

let JPEmotionCollectionViewCellID = "JPEmotionCollectionViewCellID"


@available(iOS 9.0, *)
class JPComposeEmotionKeyBoard: UIView {

    //  底部的工具栏
    @available(iOS 9.0, *)
    fileprivate lazy var emotionToolBar: JPEmotionToolBar = {
        let toolBar = JPEmotionToolBar()
        return toolBar
    }()
    
    lazy var pageControl: UIPageControl = {
        let ctr = UIPageControl()
        ctr.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
        ctr.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        ctr.hidesForSinglePage = true
        
        return ctr
    }()
    
    //  collectionView
    lazy var emotionCollectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .horizontal
        
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
        collectionView.bounces = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.isPagingEnabled = true
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = self.backgroundColor
        
        return collectionView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        emotionCollectionView.register(JPEmotionCollectionCell.self, forCellWithReuseIdentifier: JPEmotionCollectionViewCellID)
        
        DispatchQueue.main.async {
            let indexPath = IndexPath(item: 0, section: 1)
            self.emotionCollectionView.scrollToItem(at: indexPath, at: .left, animated: false)
        }
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        addSubview(emotionCollectionView)
        addSubview(emotionToolBar)
        addSubview(pageControl)
        
        pageControl.snp_makeConstraints { (make) in
            make.centerX.bottom.equalTo(emotionCollectionView)
            make.height.equalTo(10)
        }
        
        emotionToolBar.snp_makeConstraints { (make) in
            make.width.equalTo(self)
            make.bottom.left.equalTo(self)
            make.height.equalTo(35)
        }
        
        emotionCollectionView.snp_makeConstraints { (make) in
            make.left.right.top.equalTo(self)
            make.bottom.equalTo(emotionToolBar.snp_top)
        }
        
        emotionToolBar.toolBarCallBack = {[weak self] (type) in
            
            var indexPath = IndexPath()
            
            switch type {
            case .recent:
                print("最近")
                indexPath = IndexPath(item: 0, section: type.rawValue - 1000)
            case .normal:
                print("默认")
                indexPath = IndexPath(item: 0, section: type.rawValue - 1000)
            case .emoji:
                print("emoji")
                indexPath = IndexPath(item: 0, section: type.rawValue - 1000)
            case .smallFlower:
                print("浪小花")
                indexPath = IndexPath(item: 0, section: type.rawValue - 1000)
            
            }
            
            self?.emotionCollectionView.scrollToItem(at: indexPath, at: .left, animated: false)
            
            self?.setupPageControlData(indexPath: indexPath)
        }
    }
    
    //  设置pageControl的数据
    func setupPageControlData(indexPath: IndexPath) {
        pageControl.numberOfPages = JPEmotionTool.sharedEmotionTool.allEmotionArr[indexPath.section].count
        pageControl.currentPage = indexPath.item
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let flowLayout = emotionCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        
        flowLayout.itemSize = emotionCollectionView.size
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        
    }

}


// MARK: -- 数据源和代理
@available(iOS 9.0, *)
extension JPComposeEmotionKeyBoard: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return JPEmotionTool.sharedEmotionTool.allEmotionArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return JPEmotionTool.sharedEmotionTool.allEmotionArr[section].count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: JPEmotionCollectionViewCellID, for: indexPath) as! JPEmotionCollectionCell
        cell.emotions = JPEmotionTool.sharedEmotionTool.allEmotionArr[indexPath.section][indexPath.item]
        
        return cell
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let pointInView = self.convert(emotionCollectionView.center, to: emotionCollectionView)
        
        let indexPath = emotionCollectionView.indexPathForItem(at: pointInView)
        

        emotionToolBar.selectedButton(section: (indexPath?.section)!)
        setupPageControlData(indexPath: indexPath!)
    }
}
